package com.zjht.gmcc.servlet.sys;

/* 
 * @author 朱家进  
 * @version 1.4
 * ���ܣ���֯社会渠道供货平台设置
 * 2006-12-27
 */

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyJDBC;


public class SocietyToProvider extends BaseServlet {
	private static final long serialVersionUID = 1L;



	public boolean process(HttpServletRequest request, HttpServletResponse response,HttpSession session, PrintWriter out, MyJDBC myJDBC,  String menu)throws Exception {
		int i_flag = 0;
		String s_orgacodeqj = request.getParameter("orgacode").toString();// 区级别移动
		String url = "";

		url = "/sys/society/societytoprovider/societytoprovidermx.jsp?orgacode=" + s_orgacodeqj;

		String s_sql = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet rs2 = null;

		int Iicount = Integer.parseInt(request.getParameter("icount").toString());

		String Sdyzjpt, Spt, Syyt;

		s_sql = "delete from SocietyToPlant where SocietyOrgaCode = ?";
		ps = myJDBC.prepareStatement(s_sql);

		String s_sql1 = "insert into SocietyToPlant (SocietyOrgaCode,ProviderOrgaCode,PlantOrgaCode,status) VALUES(?,?,?,1)";
		PreparedStatement ps1 = myJDBC.prepareStatement(s_sql1);

		// 修改库存或补货信息(社会渠道)
		String s_sql2 = "UPDATE REQUIRE SET Status=8 WHERE ObjOrgaCode= ? AND Status=6";
		PreparedStatement ps2 = myJDBC.prepareStatement(s_sql2);

		// 修改库存或补货信息(社会渠道)
		String s_sql3 = "UPDATE REQUIRE SET Status=9 WHERE ObjOrgaCode= ? AND Status=7";
		PreparedStatement ps3 = myJDBC.prepareStatement(s_sql3);

		// 统计该厅在库存或补货当中,是否有数据,如果有不能修改对应的厅(社会渠道)
		String s_sql4 = "SELECT SUM(C) FROM ("
				+ "SELECT count(*) AS C FROM IMEI WHERE OrgaCode= ? "
				+ // 本市平台是否有库存或补货
				"union all "
				+ "SELECT count(*) AS C FROM IMEI WHERE OrgaCode IN (SELECT ORGACODE FROM ORGANIZA WHERE PARENTORGACODE = ?) "
				+ ") ";// 本市平台以下厅是否有库存或补货
		PreparedStatement ps4 = myJDBC.prepareStatement(s_sql4);

		// 把历史数据插入到另外一张表
		String s_sql5 = "INSERT INTO SocietyToPlantOld (SocietyOrgaCode,ProviderOrgaCode,PlantOrgaCode,ENDDATE) "
				+ "( SELECT SocietyOrgaCode,ProviderOrgaCode,PlantOrgaCode,SYSDATE FROM SocietyToPlant WHERE "
				+ "SocietyOrgaCode = ? )";
		PreparedStatement ps5 = myJDBC.prepareStatement(s_sql5);

		// 修改当前表的信息
		String s_sql6 = "UPDATE SocietyToPlant SET ProviderOrgaCode = ?,PlantOrgaCode = ? WHERE SocietyOrgaCode = ? )";
		PreparedStatement ps6 = myJDBC.prepareStatement(s_sql6);

		// 判断该服务厅是否设置过相对应的平台
		String s_sql7 = "SELECT COUNT(*) FROM  SocietyToPlant WHERE SocietyOrgaCode = ? ";
		PreparedStatement ps7 = myJDBC.prepareStatement(s_sql7);

		String s_sql8 = "SELECT ORGANAME FROM ORGANIZA WHERE ORGACODE = ?";
		PreparedStatement ps8 = myJDBC.prepareStatement(s_sql8);
		int count = 0;

		for (int i = 1; i <= Iicount; i++) {
			Sdyzjpt = request.getParameter("dyzjpt" + Integer.toString(i)).toString();// 修改后的对应组织
			Spt = request.getParameter("pt" + Integer.toString(i)).toString();// 原来对应的关系
			Syyt = request.getParameter("yyt" + Integer.toString(i)).toString();

			ps7.setString(1, Syyt);
			rs = ps7.executeQuery();
			while (rs.next()) {
				count = rs.getInt(1);
			}

			if (!Sdyzjpt.equals(Spt)) // 如果相等就不需要修改,否则更改
			{
				// 判断厅是否有库存信息
				ps4.setString(1, Syyt);
				ps4.setString(2, Syyt);
				rs = ps4.executeQuery();
				rs.next();

				if (rs.getInt(1) != 0) {
					i_flag = 1;
					ps8.setString(1, Syyt);
					rs2 = ps8.executeQuery();
					rs2.next();
					alert(out,"该<" + rs2.getString("ORGANAME") + ">组织有库存或补货信息,不能修改供货平台,请联系系统管理员", url);
					return false;
				} else {
					if (count != 0)// 该厅已经设置过对应的平台,之后再修改对应的平台
					{
						// 修改库存,补货等信息
						ps2.setString(1, Syyt);
						ps2.executeUpdate();

						ps3.setString(1, Syyt);
						ps3.executeUpdate();

						ps5.setString(1, Syyt);
						ps5.executeUpdate();

						ps6.setString(1, Sdyzjpt.substring(0, Sdyzjpt.indexOf(",")));
						ps6.setString(1, Sdyzjpt.substring(Sdyzjpt.indexOf(",") + 1, Sdyzjpt
								.length()));
						ps6.setString(3, Syyt);
					}
					// *****************
					// 以下为正常操作
					if ("00".equals(Sdyzjpt))// 删除
					{
						ps.setString(1, Syyt);
						ps.executeUpdate();
						i_flag = 1;
					} else {
						ps.setString(1, Syyt);
						ps.executeUpdate();

						ps1.setString(1, Syyt);
						ps1.setString(2, Sdyzjpt.substring(0, Sdyzjpt.indexOf(",")));
						ps1.setString(3, Sdyzjpt.substring(Sdyzjpt.indexOf(",") + 1, Sdyzjpt
								.length()));
						i_flag = ps1.executeUpdate();

						if (i_flag == 0) {
							break;
						}
					}
				}

			}
		}
		rs.close();

		s_sql = null;
		ps.close();
		ps = null;

		s_sql1 = null;
		ps1.close();
		ps1 = null;

		ps2.close();
		ps3.close();
		ps4.close();
		ps5.close();
		ps6.close();

		String act = "社会渠道供货平台设置";
		if (i_flag > 0) {
			alert(out, act + "成功",url);
		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return i_flag > 0;
	}
}
